import 'package:flutter_admin/api/odoo/purchase_evaluation_template_odoo_api.dart';
import 'package:flutter_admin/models/odoo/purchase_evaluation_template.dart';
import 'package:flutter_admin/pages/common/cry/cry_all.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/pages/common/cry/utils/cry_utils.dart';
import '../../../generated/l10n.dart';

class PurchaseEvaluationTemplateEdit extends StatefulWidget {
  PurchaseEvaluationTemplateEdit({this.purchaseEvaluationTemplate});

  final PurchaseEvaluationTemplate? purchaseEvaluationTemplate;

  @override
  _PurchaseEvaluationTemplateEditState createState() =>
      _PurchaseEvaluationTemplateEditState();
}

class _PurchaseEvaluationTemplateEditState
    extends State<PurchaseEvaluationTemplateEdit> {
  late PurchaseEvaluationTemplate purchaseEvaluationTemplate;
  bool? isAdd;

  final GlobalKey<FormState> formKey = GlobalKey<FormState>();
  @override
  void initState() {
    super.initState();
    this.isAdd = widget.purchaseEvaluationTemplate == null;
    this.purchaseEvaluationTemplate =
        widget.purchaseEvaluationTemplate ?? PurchaseEvaluationTemplate();
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          CryInput(
            width: 300,
            label: '模板名称',
            value: purchaseEvaluationTemplate.templateName,
            onSaved: (v) {
              purchaseEvaluationTemplate.templateName = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          CryInput(
            width: 300,
            label: '模板说明',
            value: purchaseEvaluationTemplate.templateComment,
            onSaved: (v) {
              purchaseEvaluationTemplate.templateComment = v;
            },
          ),
        ],
      ),
    );
    var buttonBar = ButtonBar(
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        CryButton(
          label: S.of(context).save,
          onPressed: () {
            FormState form = formKey.currentState!;
            if (!form.validate()) {
              return;
            }
            form.save();
            PurchaseEvaluationTemplateOdooApi.saveOrUpdate(
                    purchaseEvaluationTemplate.toOdooMap())
                .then((ResponseBodyApi res) {
              if (!res.success!) {
                //CryUtils.message(re s.message!);
                return;
              }
              Navigator.pop(context, true);
              CryUtils.message(S.of(context).saved);
            });
            // });
          },
          iconData: Icons.save,
        ),
        CryButton(
          label: S.of(context).cancel,
          onPressed: () {
            Navigator.pop(context);
          },
          iconData: Icons.cancel,
        )
      ],
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text(this.isAdd! ? S.of(context).add : S.of(context).modify),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [form],
        ),
      ),
      bottomNavigationBar: buttonBar,
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
    // return result;
  }
}
